﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using Wangzeran.CodeMaker.Core.Model;
using Wangzeran.CodeMaker.Core.DB;

namespace Wangzeran.CodeMaker.Core.Config
{
    /// <summary>
    /// 数据类型节点处理
    /// </summary>
    public class DataTypeSectionHandler : IConfigurationSectionHandler
    {
        /// <summary>
        /// 语言节点
        /// </summary>
        private const string LanguageNode = "language";
        /// <summary>
        /// 语言节点
        /// </summary>
        private const string TypeNode = "type";
        /// <summary>
        /// 名称属性
        /// </summary>
        private const string NameAttribute = "name";
        /// <summary>
        /// 数据库类型属性
        /// </summary>
        private const string DataTypeAttribute = "dataType";
        /// <summary>
        /// 目标类型属性
        /// </summary>
        private const string TargetTypeAttribute = "targetType";

        public object Create(object parent, object configContext, XmlNode section)
        {
            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    if (node.Name == LanguageNode)
                    {
                        DataTypeModal dateType = new DataTypeModal();
                        dateType.DateTypes = new Dictionary<string, string>();
                        // 设置语言名称
                        dateType.LanguageName = node.Attributes[NameAttribute].Value;
                        foreach (XmlNode childNode in node.ChildNodes)
                        {
                            if (childNode.Name != TypeNode)
                            {
                                continue;
                            }
                            // 获取数据库类型
                            string dbType = childNode.Attributes[DataTypeAttribute].Value;
                            // 获取目标类型
                            string targetType = childNode.Attributes[TargetTypeAttribute].Value;
                            dateType.DateTypes.Add(dbType, targetType);
                        }
                        DataTypeSupport.RegisterDataType(dateType);
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

            return null;

        }

    }
}
